<template>
  <div>

    <el-container>
      <el-header style="background-color: #60c2c2">
        <img src="@/assets/logo.png" alt="" style="width: 35px; position: relative; top: 10px">
        <span style="font-size: 20px; margin-left: 15px; color: white">图书管理系统</span>
        <el-dropdown style="float: right; height: 60px; line-height: 60px">
          <div style="display: flex; align-items: center; cursor: default">
            <img :src="'http://localhost:9090/api/files/' + user.img || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" alt="" style="width: 40px; height: 40px; border-radius: 50%; margin: 0 10px">
            <span style="color: white">{{ user.name }}</span>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <div @click="$router.push('/person')">个人信息</div>
            </el-dropdown-item>
            <el-dropdown-item>
              <div @click="logout">退出登录</div>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
    </el-container>

    <el-container>
      <el-aside style="overflow: hidden; min-height: 100vh; background-color: #545c64; width: 200px">
        <el-menu :default-active="$route.path" router background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
          <el-menu-item index="/">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>

          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-message"></i><span>信息管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/notice">公告信息</el-menu-item>
              <el-menu-item index="/log">操作日志</el-menu-item>
            </el-menu-item-group>
          </el-submenu>

          <el-submenu index="2" v-if="user.role === 'ADMIN'">
            <template slot="title">
              <i class="el-icon-user"></i><span>用户管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/admin">用户信息</el-menu-item>
<!--              <el-menu-item index="/person">个人中心</el-menu-item>-->
            </el-menu-item-group>
          </el-submenu>

          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-collection"></i><span>图书管理</span>
            </template>
            <el-menu-item-group>
              <el-menu-item index="/book">图书信息</el-menu-item>
              <el-menu-item index="/type">图书分类</el-menu-item>
              <el-menu-item index="/borrow">借阅信息</el-menu-item>
            </el-menu-item-group>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view @update:user="updateUser" />
      </el-main>
    </el-container>

  </div>

</template>

<script>
export default {
  name: "Layout",
  data() {
    return {
          user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
    }
  },
  created() {

  },
  methods: {
    updateUser(user) {   // 获取子组件传过来的数据  更新当前页面的数据
      this.user = JSON.parse(JSON.stringify(user))  // 让父级的对象跟子级的对象毫无关联
    },
    logout() {
      localStorage.removeItem("user");
      this.$router.push("/login");
    }
  }
}
</script>

<style scoped>
.el-menu{
  border-right: none !important;
}
</style>